﻿using NaireSystem.Api;
using NaireSystem.Api.VO;
using NaireSystem.Datas;
using NaireSystem.Enums;
using NaireSystem.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TYControls;

namespace NaireSystem.Views.UserManager
{
    /// <summary>
    /// CtrlUserManager.xaml 的交互逻辑
    /// </summary>
    public partial class CtrlUserManager : TYUserControl
    {
        ViewModelUserManager vm = new ViewModelUserManager();
        public CtrlUserManager()
        {
            InitializeComponent();
            this.DataContext = vm;
            this.Loaded += CtrlUserManager_Loaded;
        }

        private async void CtrlUserManager_Loaded(object sender, RoutedEventArgs e)
        {
            //先加载
            var rData = await Run(() =>
            {
                return DictDataApi.GetList(new DBEntity.Tbl_DictData { });
            });
            if (rData.code != 0)
            {
                this.ShowMsg(rData.msg);
                return;
            }
            var groupDatas = rData.data.GroupBy(x => x.dict_type);
            foreach (var item in groupDatas)
            {
                if (!Enum.TryParse(item.Key, out EnumDictType dictType))
                {
                    continue;
                }
                AppDictData.Update(dictType, item.ToList());
            }
        }

        private void BtnClick_Search(object sender, RoutedEventArgs e)
        {
            pager.CurrentPage = 1;
            Pagination_CurrentPageChanged(null, null);
        }

        private async void Pagination_CurrentPageChanged(object sender, Panuon.WPF.SelectedValueChangedRoutedEventArgs<int> e)
        {
            //getUserInfoList
            int pageIndex = pager.CurrentPage;
            UserInfoVo userInfo = new UserInfoVo()
            {
                user_name = vm.UserName,
            };
            vm.ListUserInfo.Clear();
            var rData = await Run(() =>
            {
                return UserApi.GetUserInfoList(new PageInfo(pageIndex, 20), userInfo);
            });
            if (rData.code != 0)
            {
                this.ShowMsg(rData.msg);
                return;
            }
            pager.MaxPage = rData.page.PageCount();
            rData.data.ForEach(x =>
            {
                var model = UIDataHelper.DataAsUI(x);
                model.Index = vm.ListUserInfo.Count + 1;
                vm.ListUserInfo.Add(model);
            });
        }

        private void BtnClick_EditUserInfo(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as UserInfoModel;
            ViewUserInfoEdit view = new ViewUserInfoEdit(dx);
            view.ShowDialog();
            Pagination_CurrentPageChanged(null, null);
        }
    }
}
